package com.example.myapplication.recyclerview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.listview.Bean;
import com.example.myapplication.listview.MyAdapter;

import java.util.List;

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.RecyclerViewViewHolder> {
    private List<Bean> date;
    private Context context;

    public RecyclerviewAdapter(List<Bean> date, Context context) {
        this.date = date;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerViewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.list_item,null);
        return new RecyclerViewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewViewHolder holder, int position) {
       holder.tv.setText(date.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return date==null?0:date.size();
    }

    public class RecyclerViewViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        public RecyclerViewViewHolder(@NonNull View itemView) {
            super(itemView);
            tv =itemView.findViewById(R.id.tv);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onRecyclerItemClickListener!=null){
                        onRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
    private OnRecyclerItemClickListener onRecyclerItemClickListener;

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener){
        onRecyclerItemClickListener = listener;
    }

   public interface OnRecyclerItemClickListener{
        void onRecyclerItemClick(int position);
   }
}
